package com.codemart.controller;

import com.codemart.common.R;
import com.codemart.constant.MessageConstant;
import com.codemart.dto.UserDetailDTO;
import com.codemart.dto.UserUpdatePasswordDTO;
import com.codemart.service.UserDetailService;
import com.codemart.service.UserInfoService;
import com.codemart.vo.UserVO;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Objects;

@Slf4j
@CrossOrigin
@RestController
@RequestMapping("/user")
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    UserDetailService userDetailService;

    @GetMapping("/userInfo")
    public R userInfo(HttpServletRequest request){

        return R.success(userInfoService.userInfo(request));

    }

    @GetMapping("/userDetail")
    public R userDetail(HttpServletRequest request){

        return R.success(userDetailService.getUserDetail(request));
    }

    @GetMapping("/getMyProject")
    public R getMyProject(HttpServletRequest request){

        return R.success(userInfoService.getMyProject(request));
    }

    @PostMapping("/updateUserDetail")
    public R updateUserDetail(HttpServletRequest request, @RequestBody UserDetailDTO ud){
        userInfoService.updateUserDetail(request,ud);
        return R.success();
    }

    @PostMapping("/listMyBidding")
    public R listMyBidding(HttpServletRequest request){
        return R.success(userInfoService.listMyBidding(request));
    }

    @PostMapping("/updatePassword")
    public R updatePassword(HttpServletRequest request,@RequestBody UserUpdatePasswordDTO userUpdatePasswordDTO){
        if(userUpdatePasswordDTO == null ||
           Objects.equals(userUpdatePasswordDTO.getNewPWD(), "") ||
           userUpdatePasswordDTO.getNewPWD() == null ||
           Objects.equals(userUpdatePasswordDTO.getNewPWD(), " ") ||
           Objects.equals(userUpdatePasswordDTO.getOldPWD(), "") ||
           userUpdatePasswordDTO.getOldPWD() == null ||
           Objects.equals(userUpdatePasswordDTO.getNewPWD(), " ")){
            return R.error(MessageConstant.OBJECT_IS_NULL);
        }
        userDetailService.updatePassword(request,userUpdatePasswordDTO);
        return R.success(MessageConstant.UPDATE_SUCCESS);
    }


}
